#include "aboutwidget.h"
#include <QDebug>
#include <QScreen>
#include <QRect>
#include <QGuiApplication>
#include <QDesktopServices>
#include <QApplication>
#include <QCoreApplication>
#include "customstyle.h"
#include "util/xatom-helper.h"
#define VERSION "1.1.3"
QGSettings *AboutWidget::m_pGsettingThemeData;
AboutWidget::AboutWidget(const StyleWidgetAttribute& swa, QString dialogTitleText, bool isDialog ,QWidget *parent)
    :QDialog(parent),local_swa(swa)
{
    Q_UNUSED(dialogTitleText);
    m_isDialog=isDialog;
    m_pGsettingThemeData = new QGSettings(FITTHEMEWINDOW);  //安装窗口
//    this->setWindowIcon(QIcon("/usr/share/icons/ukui-icon-theme-default/64x64@2x/appskylin-camera.png"));
//    this->setWindowIcon(QIcon(":/image/camera-app"));  //lhl debug 更改关于页面图标
//    this->setWindowFlag(Qt::Tool);
    this->setWindowModality(Qt::ApplicationModal); //窗口对应用程序是模态的，并阻止对所有窗口的输入
    this->setAttribute(Qt::WA_ShowModal,true);
#ifndef __V10__
    //添加窗管协议
    MotifWmHints hints;
    hints.flags = MWM_HINTS_FUNCTIONS|MWM_HINTS_DECORATIONS;
    hints.functions = MWM_FUNC_ALL;
    hints.decorations = MWM_DECOR_BORDER;
    XAtomHelper::getInstance()->setWindowMotifHint(winId(), hints);
#endif
    initGsetting();
 //   this->setWindowTitle(tr("Camera"));
//  this->setWindowModality(Qt::WindowModal);

    QRect availableGeometry = qApp->primaryScreen()->availableGeometry();
    this->move((availableGeometry.width() - this->width())/2,(availableGeometry.height() - this->height())/2);
    //只显示关闭
    this->setWindowFlags(Qt::CustomizeWindowHint | Qt::WindowCloseButtonHint);
    myStyle(swa);
}

AboutWidget::~AboutWidget()
{
    aboutWeigetPageTitle->deleteLater();
}

void AboutWidget::WidgetStyleClose()
{
    //emit doSomethig();
    emit allClose();
    this->close();
}


void AboutWidget::myStyle(StyleWidgetAttribute swa)
{

    QVBoxLayout *aboutWeightLayout =new QVBoxLayout();  //整体布局
    aboutWeigetPageTitle = new SettingPageTitle(this);
    QVBoxLayout *aboutWeightTitleLayout = new QVBoxLayout();   //标题栏布局
    aboutWeightTitleLayout->addWidget(aboutWeigetPageTitle);

    //窗口设置
//    this->setWindowFlags(Qt::FramelessWindowHint);// | Qt::Popup);//无边框
//    this->setAttribute(Qt::WA_TranslucentBackground, true);//窗体透明
      this->setFixedSize(swa.w,swa.h);

    //控件
    title = new QWidget;//标题栏
    title->setMaximumHeight(swa.titleHeight);
    title->setMinimumHeight(swa.titleHeight);
    title->setObjectName("title");

    body = new QWidget;//窗体
    body->setFixedHeight(this->height()-title->height()-swa.shadow*2);
    body->setObjectName("body");

//    QSize smallWidgetSize(30,30);//小按钮大小   //lhl debug 屏蔽

//    widgetClose =new QPushButton;//关闭按钮
//    widgetClose->setObjectName("widgetClose");
//    widgetClose->setIconSize(smallWidgetSize);
//    widgetClose->setFixedSize(smallWidgetSize);
//    connect(widgetClose,&QPushButton::clicked,this,&AboutWidget::WidgetStyleClose);

//    //布局
    QHBoxLayout *hlt0=new QHBoxLayout;//右上角按钮内部，水平布局
    hlt0->setMargin(0);
    hlt0->setSpacing(0);

    QHBoxLayout *hlt=new QHBoxLayout;//标题栏内部，水平布局
    hlt->setMargin(0);
    hlt->setSpacing(0);
    hlt->addStretch(99);
    hlt->addLayout(hlt0,1);
    hlt->addSpacing(8);

    QVBoxLayout *vlt_temp=new QVBoxLayout;//标题栏内部，垂直布局
    vlt_temp->setMargin(0);
    vlt_temp->setSpacing(0);
    vlt_temp->addSpacing(6);
    vlt_temp->addLayout(hlt,1);
    vlt_temp->addStretch(99);

    title->setLayout(vlt_temp);

    QHBoxLayout *hlt2=new QHBoxLayout;//标题栏外部
    hlt2->setMargin(0);
    hlt2->setSpacing(0);
    hlt2->addSpacing(swa.shadow);
   // hlt2->addWidget(title);        //原代码设置了title 未设置内容 新设立
    hlt2->addSpacing(swa.shadow);

   // aboutIcon = new QLabel;
    aboutIcon = new QPushButton(this);
    aboutIcon->setIcon(QIcon::fromTheme("kylin-camera"));

//    aboutIcon->setPixmap(QPixmap::fromImage(QImage("/usr/share/icons/ukui-icon-theme-default/64x64@2x/apps/kylin-camera.png")));
 //   aboutIcon->setPixmap(QPixmap(":/image/kylin-camera.png"));   //关于页面内部的图片
#ifdef __V10__
    aboutIcon->setPixmap(QPixmap::fromImage(QImage(":/image/kylin-camera")));
#endif
    //aboutIcon->setScaledContents(true);
    //aboutIcon->setFixedSize(96,96);
    aboutIcon->setFixedSize(88,88);
    aboutIcon->setIconSize(QSize(96,96));

    aboutText = new QLabel;

    aboutText->setText(tr("kylin-camera"));
//    aboutText->setStyleSheet("font-size:25px;");

    aboutVersion_1 = new QLabel;
    aboutVersion_1->setText(tr("Version:"));
    aboutVersion_2 = new QLabel;
    aboutVersion_2->setText(VERSION);

    about = new QTextEdit;

    QPalette pal = QApplication::style()->standardPalette();  //默认调色板
//    QPalette pal = about->palette();
//    pal.setBrush(QPalette::Base, Qt::white);

    about->setText(tr("Camera is a very strong camera software. "
                      "It has the characteristics of easy to use. "
                      "You can realize the function of taking a photo recording and displaying the album")
                    );
//    about->setPalette(pal);
//    about->setFrameShape(QFrame::NoFrame);

    aboutMail_1 = new QLabel;
//    aboutMail_1->setText(tr("Service & Support: ") +
//                         "<a href=\"mailto://support@kylinos.cn\""
//                         "style=\"color:palette(buttonText)\">"
//                         "support@kylinos.cn</a>");

//    m_pGsettingThemeData = new QGSettings(FITTHEMEWINDOW);
    nowThemeStyle = m_pGsettingThemeData->get("styleName").toString();

    if("ukui-dark" == nowThemeStyle || "ukui-black" == nowThemeStyle){
        aboutMail_1->setText(tr("Service & Support: ") +
                             "<a href=\"mailto://support@kylinos.cn\""
                             "style=\"color:rgba(225,225,225,1)\">"
                             "support@kylinos.cn</a>");
    }
    else{ //
        aboutMail_1->setText(tr("Service & Support: ") +
                             "<a href=\"mailto://support@kylinos.cn\""
                             "style=\"color:rgba(0,0,0,1)\">"
                             "support@kylinos.cn</a>");
    }

    connect(aboutMail_1, &QLabel::linkActivated, this, [=](const QString url){
          QDesktopServices::openUrl(QUrl(url));
      });
    aboutMail_1->setContextMenuPolicy(Qt::NoContextMenu);

    QHBoxLayout *hlt_Icon = new QHBoxLayout;
    hlt_Icon->setMargin(0);
    hlt_Icon->setSpacing(0);
    hlt_Icon->addStretch(99);
    hlt_Icon->addWidget(aboutIcon,1);
    hlt_Icon->addStretch(99);

    QHBoxLayout *hlt_Text=new QHBoxLayout;
    hlt_Text->setMargin(0);
    hlt_Text->setSpacing(0);
    hlt_Text->addStretch(99);
    hlt_Text->addWidget(aboutText,1);
    hlt_Text->addStretch(99);

    QHBoxLayout *hlt_About = new QHBoxLayout;
    hlt_About->setMargin(0);
//    hlt_About->setSpacing(0);
    hlt_About->addSpacing(20);
    hlt_About->addWidget(about,1);
    hlt_About->addSpacing(20);

    QHBoxLayout *hlt_Ver=new QHBoxLayout;
    hlt_Ver->setMargin(0);
    hlt_Ver->setSpacing(0);
    hlt_Ver->addStretch(99);
    hlt_Ver->addWidget(aboutVersion_1,1);
    hlt_Ver->addSpacing(5);
    hlt_Ver->addWidget(aboutVersion_2,1);
    hlt_Ver->addStretch(99);

    QHBoxLayout *hlt_Mail = new QHBoxLayout;
    hlt_Mail->setMargin(0);
    hlt_Mail->setSpacing(0);
    hlt_Mail->addStretch(99);
    hlt_Mail->addWidget(aboutMail_1);
    hlt_Mail->addStretch(99);


    QVBoxLayout *vlt_body=new QVBoxLayout;
    vlt_body->setMargin(0);
    vlt_body->setSpacing(0);
    vlt_body->addStretch(12);
    vlt_body->addLayout(hlt_Icon);
    vlt_body->addStretch(12);
    vlt_body->addLayout(hlt_Text);
//    vlt_body->addStretch(12);
    vlt_body->addLayout(hlt_Ver);
//    vlt_body->addLayout(hlt_About);
//    vlt_body->addStretch(12);
    vlt_body->addLayout(hlt_Mail);
    vlt_body->addStretch(12);

    body->setLayout(vlt_body);

    QHBoxLayout *hlt3=new QHBoxLayout;//窗体
    hlt3->setMargin(0);
    hlt3->setSpacing(0);
//    hlt3->addSpacing(swa.shadow);
    hlt3->addWidget(body);
//    hlt3->addSpacing(swa.shadow);

    QVBoxLayout *vl=new QVBoxLayout;//内部显示整体
    vl->setMargin(0);
    vl->setSpacing(0);
    vl->addSpacing(swa.shadow);
    vl->addLayout(hlt2);
    vl->addLayout(hlt3);
    vl->addStretch(99);
    vl->addSpacing(swa.shadow);

    aboutWeightLayout->setMargin(5);
    aboutWeightLayout->setSpacing(0);
    aboutWeightLayout->addLayout(aboutWeightTitleLayout);
    aboutWeightLayout->addLayout(vl);

    this->setLayout(aboutWeightLayout);


    //增加标题栏
   // this->setPalette(pal);
}



void AboutWidget::showOrHide()
{
    if(this->isHidden())
    {
        this->show();
    }
    else
    {
        this->hide();
    }
}

void AboutWidget::initGsetting(){
    if(QGSettings::isSchemaInstalled(FITTHEMEWINDOW)){
        connect(m_pGsettingThemeData,&QGSettings::changed,this,&AboutWidget::dealSystemGsettingChange);
    }

}

void AboutWidget::dealSystemGsettingChange(const QString key){
    if(key == "styleName"){
        refreshThemeBySystemConf();
    }
}

void AboutWidget::refreshThemeBySystemConf(){
    QString themeNow = m_pGsettingThemeData->get("styleName").toString();
    if("ukui-dark" == themeNow || "ukui-black" == themeNow){
        setThemeDark();
    }else{
        setThemeLight();
    }
}

void AboutWidget::setThemeDark(){
//    emit menuModuleSetThemeStyle("dark-theme");
    aboutMail_1->setText(tr("Service & Support: ") +
                         "<a href=\"mailto://support@kylinos.cn\""
                         "style=\"color:rgba(225,225,225,1)\">"
                         "support@kylinos.cn</a>");
}

void AboutWidget::setThemeLight(){
//    emit menuModuleSetThemeStyle("light-theme");
    aboutMail_1->setText(tr("Service & Support: ") +
                         "<a href=\"mailto://support@kylinos.cn\""
                         "style=\"color:rgba(0,0,0,1)\">"
                         "support@kylinos.cn</a>");
}


void AboutWidget::ThemeChooseForWidget(const QString& str)
{

    if("ukui-dark" == str || "ukui-black" == str)
    {

        //样式表stylesheet//测试用border:1px solid red;
        QString bodyStyleSheet="QWidget{background-color:rgba(20, 20, 20, 1);border-bottom-left-radius:"+QString::number(local_swa.radius)+
                                "px;border-bottom-right-radius:"+QString::number(local_swa.radius)+"px;}";
        title->setStyleSheet("QWidget{background-color:rgba(20, 20, 20, 1);border-top-left-radius:"
                                 +QString::number(local_swa.radius)+"px;border-top-right-radius:"+QString::number(local_swa.radius)+"px;}");
        body->setStyleSheet(bodyStyleSheet);


        widgetClose->setStyleSheet("QPushButton{background-color:rgba(255,255,255,0);border-image:url(:/data/close_h.png);border-radius:4px;font-size:14px;}"
                                   "QPushButton:hover{background-color:rgba(240,64,52,1);border-image:url(:/data/close_h.png);border-radius:4px;font-size:14px;}"
                                   "QPushButton:pressed{background-color:rgba(215,51,53,1);border-image:url(:/data/close_p.png);border-radius:4px;font-size:14px;}");
     }
    else
    {

       //样式表stylesheet//测试用border:1px solid red;
       QString bodyStyleSheet="QWidget{background-color:rgba(255,255,255,1);border-bottom-left-radius:"+QString::number(local_swa.radius)+
                               "px;border-bottom-right-radius:"+QString::number(local_swa.radius)+"px;}";
       title->setStyleSheet("QWidget{background-color:rgba(255,255,255,1);border-top-left-radius:"
                                +QString::number(local_swa.radius)+"px;border-top-right-radius:"+QString::number(local_swa.radius)+"px;}");
       body->setStyleSheet(bodyStyleSheet);


       widgetClose->setStyleSheet("QPushButton{background-color:rgba(255,255,255,0);border-image:url(:/data/close_d.png);border-radius:4px;font-size:14px;}"
                                  "QPushButton:hover{background-color:rgba(240,64,52,1);border-image:url(:/data/close_h.png);border-radius:4px;font-size:14px;}"
                                  "QPushButton:pressed{background-color:rgba(215,51,53,1);border-image:url(:/data/close_p.png);border-radius:4px;font-size:14px;}");
    }

}

